--[[
	场景管理

--]]

local SceneManager = class("SceneManager")

function SceneManager:ctor()
	self.scenes = {}
	self.cur_scene = nil --当前场景
	setmetatable(self.scenes, {__mode = "v"})
	
end

function SceneManager:getScene(name)
	for n, scene in pairs(self.scenes) do 
		if n == name then 
			return scene
		end
	end
	return nil
end

function SceneManager:getCurScene()
	return self.cur_scene
end

function SceneManager:createScene(name, ...)

	local scene = self:getScene(name)
	if scene then 
		return scene
	end
	local ok, Class = xpcall(require, debug.traceback, "ui.scenes."..name)
	if not ok or not Class then 
		print("________", ok, Class)
		return 
	end
	print("_______",name, Class)
	local scene = Class.new()	
	if scene.init then 
		scene:init(...)
	end
	self.scenes[name] = scene
	if not self.cur_scene then 
		self.cur_scene = scene
	else
		if self.cur_scene.exit then 
			self.cur_scene:exit()
		end
		self.cur_scene = scene
	end

	return scene
end

-- function SceneManager:showScene(name)
-- 	local scene = self:getScene(name)
-- 	if scene then 
-- 		scene:show()
-- 	end	
-- 	self.cur_scene = scene
-- end

-- function SceneManager:hideScene(name)
-- 	local scene = self:getScene(name)
-- 	if scene then 
-- 		scene:hide()
-- 	end	
-- end

function SceneManager:closeScene(name)
	for n, scene in pairs(self.scenes) do 
		if n==name then 
			self.scenes[name] = nil
			return
		end
	end	
end


function SceneManager:load()
	self.ui_manager = global.ui_manager
	-- self:createScene("1945")
	self:createScene("boxclip")
    self.ui_manager:createWindow("Test")

end

function SceneManager:draw()
	self.cur_scene:draw()
    self.ui_manager:draw() 
end

function SceneManager:update(dt)
	self.cur_scene:update(dt)
    self.ui_manager:update(dt) 
end

function SceneManager:mousereleased(x, y, button)
	self.cur_scene:mousereleased(x, y, button)
	self.ui_manager:mousereleased(x, y, button)
end

function SceneManager:mousepressed(x, y, button)
	self.cur_scene:mousepressed(x, y, button)
	self.ui_manager:mousepressed(x, y, button)
end

function SceneManager:textinput(text)
	self.cur_scene:textinput(text)
	self.ui_manager:textinput(text)
end
function SceneManager:keypressed(key, scancode, isrepeat)
	self.cur_scene:keypressed(key, scancode, isrepeat)
	self.ui_manager:keypressed(key, scancode, isrepeat)
end
function SceneManager:keyreleased(key, scancode)
	self.cur_scene:keyreleased(key, scancode)
	self.ui_manager:keyreleased(key, scancode)
end


return SceneManager



